package network;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class EchoClient {
	
	public static void main(String[] args) throws UnknownHostException, IOException {
		
		// 서버로 연결
		Socket sock = new Socket("127.0.0.1", 5000);
		
		// 연결이 됨
		
		OutputStream out = sock.getOutputStream();
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
		
		InputStream in = sock.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		
		Scanner sc = new Scanner(System.in);
		String dataFromServer = null;
		
		while (true) {
			System.out.print("MESSAGE: ");
			String message = sc.nextLine();
			if (message.equals("exit")) {
				break;
			}
			writer.write(message + "\n");
			writer.flush();
			
			dataFromServer = reader.readLine();
			System.out.println(dataFromServer);
		}
		
		sock.close();
		
	}
	
}
